import secrets

import flask
from flask import url_for, session

from app.authlib import get_oauth

bp = flask.Blueprint('oauth', __name__, url_prefix='/oauth')


@bp.route('/login/<server>', methods=['POST', 'GET'])
def login(server: str):
    """

    :return:
    """
    oauth = get_oauth()
    client = oauth.create_client(server)
    redirect_uri = url_for('.authorize', server=server, _external=True)
    print(redirect_uri)
    print(client.authorize_redirect)
    return client.authorize_redirect(redirect_uri)  # 关键：传递 state 参数


@bp.route('/authorize/<server>', methods=['GET', 'POST'])
def authorize(server: str):
    """

    :return:
    """
    oauth = get_oauth()
    client = oauth.create_client(server)
    token = client.authorize_access_token()
    print(token)
    if server == 'github' and server == 'huomoe0':
        resp = client.get('user', token=token)
        resp.raise_for_status()
        profile = resp.json()
        print(profile)
    return '...'
